/**
 * 
 */
package com.java.test.messagecenter.mapper;

import com.java.test.messagecenter.dto.NoticeInfo;
import com.java.test.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Administrator
 *
 */
@Component
public class NoticeMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selectpushnoticeCount(NoticeInfo info) {
		String sql = "select count(*) from message_pushnotice where 1=1 ";

		if (info.getPushTime() != null) {
			sql += " and TO_DAYS(push_time)= TO_DAYS('" + info.getPushTime() + "')";
		}

		if (info.getTitle() != null && !"".equals(info.getTitle())) {

			sql += " and title like '%" + info.getTitle() + "%'";
		}

		if (info.getNoticeType() != 0) {
			sql += " and notice_type ='" + info.getNoticeType() + "'";
		}

		Integer i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i == null ? null : i;

	}

	public List<NoticeInfo> selectpushnoticeList(NoticeInfo info, PageInfo<NoticeInfo> page) {

		String sql = "select * from message_pushnotice where 1=1 ";

		if (info.getPushTime() != null) {
			sql += " and TO_DAYS(push_time)= TO_DAYS('" + info.getPushTime() + "')";
		}

		if (info.getTitle() != null && !"".equals(info.getTitle())) {

			sql += " and title like '%" + info.getTitle() + "%'";
		}
		if (info.getNoticeType() != 0) {
			sql += " and notice_type ='" + info.getNoticeType() + "'";
		}
		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += " order by push_time desc limit ?,?";

		List<NoticeInfo> list = jdbcTemplate.query(sql, new NoticeInfo(), start, limit);

		return list;

	}

	public int updatepushnoticeInfo(NoticeInfo info) {

		String sql = "update message_pushnotice set title=?,content=?,push_time=now(),power_type=?,status=0 where id=?";

		int i = jdbcTemplate.update(sql, info.getTitle(), info.getContent(), info.getPowerType(), info.getId());

		return i;

	}

	public int deletepushnoticeInfo(int[] ids) {
		String sql = "delete from  message_pushnotice where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, ids[i]);
			}

			public int getBatchSize() {
				return ids.length;
			}
		});
		return 1;
	}

	public int addpushnoticeInfo(NoticeInfo info) {

		String sql = "insert into message_pushnotice(title,content,push_time,notice_type,power_type)values(?,?,now(),?,?)";

		int i = jdbcTemplate.update(sql, info.getTitle(), info.getContent(), info.getNoticeType(), info.getPowerType());

		return i;

	}

	public NoticeInfo selectnoticeInfo(NoticeInfo info) {
		String sql = "select * from message_pushnotice where id=?";

		NoticeInfo infos = jdbcTemplate.queryForObject(sql, new NoticeInfo(), info.getId());

		return infos;

	}

	/**
	 * 跑马灯推送查询
	 * 
	 * @author
	 * @date 2020/7/20 0020 9:54
	 * @return java.util.List<com.java.test.messagecenter.dto.HorseNoticeInfo>
	 */
	public List<NoticeInfo> selectHorseRaceLampPushList(LocalDateTime now,int noticeType) {

		String sql = "SELECT t.* FROM message_pushnotice t " + "WHERE notice_type = ? " + "AND status = 0 "
				+ "AND t.push_time <= ? ";

		List<NoticeInfo> list = jdbcTemplate.query(sql, new NoticeInfo(), noticeType,now);
		return list;
	}

	/**
	 * 更改跑马灯状态
	 * @param id
	 * @author
	 * @date 2020/7/20 0020 14:59
	 * @return int
	 */
	public int updateHorseRaceLampStatus(long id) {

		String sql = "update message_pushnotice set status=1  where id = ?";

		int i = jdbcTemplate.update(sql, id);
		return i;
	}

}
